package com.ushahidi.android.app;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.ushahidi.android.app.data.CategoriesData;
import com.ushahidi.android.app.data.IncidentsData;
import com.ushahidi.android.app.data.ResourceCategoryData;
import com.ushahidi.android.app.data.ResourceData;
import com.ushahidi.android.app.data.UshahidiDatabase;
import com.ushahidi.android.app.util.Util;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Debug;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class INEED_V2 extends DashboardActivity {

	private ListView mOptionList;
	private ListIncidentAdapter ila;
	private Bundle ResourceBundle;
	private List<CategoriesData> tmpCatePile;
	//private List<ResourceData> tmpResourcePile;
	private static final int VIEW_RESOURCE = 3;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ila = new ListIncidentAdapter(this);
		tmpCatePile = new LinkedList<CategoriesData>(); // for storing all categories from database
		//tmpResourcePile = new LinkedList<ResourceData>();
		ResourceBundle = new Bundle();
		setContentView(R.layout.i_need_v2);
		setTitleFromActivityLabel(R.id.title_text);
		mOptionList = (ListView) findViewById(R.id.resources_option_list);
		Cursor cursor = UshahidiApplication.mDb.fetchResourceCategories(); // retrieve categories data from local DB
		showResults(cursor); //show categories in list
	}

	@Override
	public void onRefreshReports(View v) {
		UshahidiApplication.mDb.onUpgrade();
	}

	// written by yujie any question please email : yujie.hank@gmail.com
	// make resourcedata out of Cursor cursor , return ResourceData.
	private void getResourceByCursor(Cursor cursor) {

		if (cursor.moveToFirst()) {

			int titleIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_TITLE);
			int idIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_ID);
			int descIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_DESC);
			int dateIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_DATE);
			int modeIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_MODE);
			int verifiedIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_VERIFIED);
			int locationIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_LOC_NAME);
			int latitudeIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_LOC_LATITUDE);
			int longitudeIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_LOC_LONGITUDE);
			int categoryIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_CATEGORIES);
			int mediaIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_MEDIA);
			int imageIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.RESOURCE_IMAGE);
			ila.removeItems();
			ila.notifyDataSetChanged();
			do {
				ResourceData resourceData = new ResourceData();
				//tmpResourcePile.add(resourceData);
				int id = Util.toInt(cursor.getString(idIndex));
				resourceData.setResourceId(id);
				resourceData.setResourceLocLatitude(cursor
						.getString(latitudeIndex));
				resourceData.setResourceLocLongitude(cursor
						.getString(longitudeIndex));

				String title = cursor.getString(titleIndex);
				resourceData.setResourceTitle(title);

				String description = cursor.getString(descIndex);
				resourceData.setResourceDesc(description);

				String categories = cursor.getString(categoryIndex);
				resourceData.setResourceCategories(categories);

				String location = cursor.getString(locationIndex);
				resourceData.setResourceLocation(location);

				String date = Util.formatDate("yyyy-MM-dd HH:mm:ss",
						cursor.getString(dateIndex),
						"MMMM dd, yyyy 'at' hh:mm:ss aaa");

				resourceData.setResourceDate(date);

				String media = cursor.getString(mediaIndex);
				resourceData.setResourceThumbnail(media);

				String thumbnails[];
				Drawable d = null;
				thumbnails = media.split(",");
				// TODO do a proper check for thumbnails
				if (!TextUtils.isEmpty(thumbnails[0])) {
					d = ImageManager.getImages(thumbnails[0]);
				} else {
					d = null;
				}

				String image = cursor.getString(imageIndex);
				resourceData.setResourceImage(image);

				String status = Util.toInt(cursor.getString(verifiedIndex)) == 0 ? getString(R.string.report_unverified)
						: getString(R.string.report_verified);
				resourceData.setResourceVerified(Util.toInt(cursor
						.getString(verifiedIndex)));

			} while (cursor.moveToNext());
		}

	}

	private void showResults(Cursor cursor) {
		String title;
		String description;
		int id;
		String thumbnails[];
		Drawable d = null;

		// Modified by OceanMaster
		// Added AroundMe Entry
		ila.removeItems();
		ila.notifyDataSetChanged();
		ListIncidentText listText = new ListIncidentText();
		listText.setId(-1);// OceanMaster, -1 to mark "Around Me"
		listText.setTitle("View Everything");
		listText.setDesc("View all resource");
		listText.setArrow(getResources().getDrawable(R.drawable.ushahidi_arrow));
		listText.setThumbnail(d == null ? getResources().getDrawable(
				R.drawable.ushahidi_view_everything_icon) : d);
		ila.addItem(listText);
		// Add end

		if (cursor.moveToFirst()) {
			// CATEGORY_ID, CATEGORY_TITLE, CATEGORY_DESC, CATEGORY_COLOR,
			// CATEGORY_IS_UNREAD, CATEGORY_TYPE
			int titleIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.CATEGORY_TITLE);
			int idIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.CATEGORY_ID);
			int descIndex = cursor
					.getColumnIndexOrThrow(UshahidiDatabase.CATEGORY_DESC);

			do {
				CategoriesData CateData = new CategoriesData();
				tmpCatePile.add(CateData);
				listText = new ListIncidentText();
				id = cursor.getInt(idIndex);
				title = cursor.getString(titleIndex);
				description = cursor.getString(descIndex);
				CateData.setCategoryId(id);
				CateData.setCategoryTitle(title);
				CateData.setCategoryDescription(description);

				listText.setId(id);
				listText.setTitle(title);
				listText.setDesc(description);
				listText.setArrow(getResources().getDrawable(
						R.drawable.ushahidi_arrow));
				switch (id) {
				case 25:// food icon
					d = getResources().getDrawable(R.drawable.icon_food);
					break;
				case 26:// drinking water icon
					d = getResources().getDrawable(
							R.drawable.icon_drinking_water);
					break;
				case 27:// shelter icon
					d = getResources().getDrawable(R.drawable.icon_shelter);
					break;
				case 29:// heavy equipment icon
					d = getResources().getDrawable(
							R.drawable.icon_heavy_equipment);
					break;
				case 31:// hospital icon
					d = getResources().getDrawable(R.drawable.icon_hospital);
					break;
				}

				listText.setThumbnail(d == null ? getResources().getDrawable(
						R.drawable.ushahidi_report_icon) : d);
				ila.addItem(listText);
			} while (cursor.moveToNext());
		}

		cursor.close();
		ila.notifyDataSetChanged();
		mOptionList.setAdapter(ila);

		mOptionList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// UshahidiApplication.mDb.onUpgrade();//upgrade local database

				// ---------------OceanMaster-------------------
				// Around Me entry
				if (position == 0) // Around Me
				{
					Log.v("Oceanmaster", "Around Me selected!!!!");
					Intent intent = new Intent(INEED_V2.this, AroundMeMap.class);
					// Intent intent = new Intent(INEED_V2.this,
					// AroundMe.class);
					// intent.putExtra("catid", catid);
					startActivityForResult(intent, VIEW_RESOURCE);
					setResult(RESULT_OK, intent);
					return;
				}
				position--; // else , index in tmpCatePile is position-1
				// Add end

				int catid = tmpCatePile.get(position).getCategoryId();
				Intent intent = new Intent(INEED_V2.this, ResourceTab.class);
				intent.putExtra("catid", catid);
				startActivityForResult(intent, VIEW_RESOURCE);
				setResult(RESULT_OK, intent);

			}

		});
	}

	// by yujie pop out map on click map button
	public void onPopMap(View v) {
		Intent intent = new Intent(INEED_V2.this, AroundMe.class);
		startActivityForResult(intent, VIEW_RESOURCE);
		setResult(RESULT_OK, intent);
	}

}
